/** 
 * @Title: CrawlCarInfoOfIFeng.java 
 * @Package com.zizo.ifeng.carinfo 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月1日 下午3:02:31 
 */
package com.zizo.ifeng.carinfo.crawl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zizo.ifeng.carinfo.dao.CarInfoDaoOfIFeng;

/**
 * @ClassName: CrawlCarInfoOfIFeng
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月1日 下午3:02:31
 * 
 */
public class CrawlCarInfoOfIFeng implements Runnable {
	private int carid;
	private String carname;
	CarInfoDaoOfIFeng dao = new CarInfoDaoOfIFeng();

	public CrawlCarInfoOfIFeng(int carid, String carname) {
		super();
		this.carid = carid;
		this.carname = carname;
	}

	/**
	 * @Title: getIFengCarInfo
	 * @Description: TODO 得到凤凰汽车的Json数据
	 * @return
	 * @return String
	 * @author Alexis
	 * @throws JSONException
	 */
	public String getIFengCarInfo() throws JSONException {
		String pathname = "C:\\Data\\carfactory\\src\\com\\zizo\\ifeng\\carinfo\\crawl\\ifengcarinfo.txt";
		StringBuffer body = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(pathname)));
			String temp = "";
			while ((temp = br.readLine()) != null) {
				body.append(temp + "\n");
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
		parseJson(body.toString());
		return body.toString();
	}

	public void parseJson(String data) throws JSONException {
		JSONArray jsonArr = new JSONArray(data);
		for (int i = 0; i < jsonArr.length(); i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			int brand_id = jsonObj.getInt("i");
			String brand_name = jsonObj.getString("n");
			JSONArray jsonArray = jsonObj.getJSONArray("s");
			for (int j = 0; j < jsonArray.length(); j++) {
				JSONObject jsonObjContent = jsonArray.getJSONObject(j);
				String oem_name = jsonObjContent.getString("n");
				JSONArray jsonArrContent = jsonObjContent.getJSONArray("b");
				for (int k = 0; k < jsonArrContent.length(); k++) {
					JSONObject car = jsonArrContent.getJSONObject(k);
					int car_id = car.getInt("i");
					String car_name = car.getString("n");
					System.out
							.println(brand_id + "\t" + brand_name + "\t" + oem_name + "\t" + car_id + "\t" + car_name);
					// Download(car_id, car_name);
					// IFengCarInfo iFengCarInfo = new IFengCarInfo(brand_id,
					// brand_name, oem_name, car_id, car_name);
					// dao.insertCarInfo(iFengCarInfo);
				}
			}
		}
	}

	/***
	 * 下载图片
	 * 
	 * @param listImgSrc
	 */
	public void Download(int id, String name) {
		String url = "http://a0.ifengimg.com/autoimg/serial/180/" + id + "_3.png";
		try {
			URL uri = new URL(url);
			InputStream in = uri.openStream();
			FileOutputStream fo = new FileOutputStream(new File("C:/img1/" + id + ".png"));
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = in.read(buf, 0, buf.length)) != -1) {
				fo.write(buf, 0, length);
			}
			in.close();
			fo.close();
			// System.out.println(imageName + "完成");
		} catch (Exception e) {
			System.out.println(id);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		Download(carid, carname);
	}

	public static void main(String[] args) throws Exception {
		CarInfoDaoOfIFeng dao = new CarInfoDaoOfIFeng();
		List<String> list = dao.getIDS();
		ExecutorService fix = Executors.newFixedThreadPool(15);
		for (String car : list) {
			int id = Integer.parseInt(car.split("_")[0]);
			String name = car.split("_")[1];
			fix.execute(new CrawlCarInfoOfIFeng(id, name));
		}
		fix.shutdown();
	}

}
